package org.example.controller;

import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.example.domain.ResponseResult;
import org.example.domain.dto.ArticleListDto;
import org.example.domain.entity.Article;
import org.example.enums.AppHttpCodeEnum;
import org.example.exception.SystemException;
import org.example.service.ArticleService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
@RequiredArgsConstructor
public class ArticleController {

    private final ArticleService articleService;

    @PostMapping("/addArticle")
    public ResponseResult addArticle(@RequestBody Article article){
        if(StringUtils.isBlank(article.getTitle())){
            throw new SystemException(AppHttpCodeEnum.NOT_CATEGORY_NAME_ERROR);
        }
        article.setSummary(article.getSummary().replaceAll(" +",""));
        return articleService.addArticle(article);

    }


    @PostMapping("/getArticleList")
    public ResponseResult getArticleList(@RequestBody ArticleListDto articleListDto){

        return articleService.getArticleList(articleListDto);
    }

    @PutMapping("/updateArticle")
    public ResponseResult updateArticle(@RequestBody Article article){
        if(StringUtils.isBlank(article.getTitle())){
            throw new SystemException(AppHttpCodeEnum.NOT_CATEGORY_NAME_ERROR);
        }
        return articleService.updateArticle(article);
    }
}
